package com.lmpop.frame.dao;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.pojo.Menuinfo;
import com.lmpop.frame.util.RandomID;

public class MenuInfo implements TradeInterf
{
  QueryList queryList;
  RandomID randomId;

  public MenuInfo()
  {
    this.queryList = new QueryList();
    this.randomId = new RandomID();
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    return null;
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ts_menuinfo ts_menuinfo = (Ts_menuinfo)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ts_menuinfo.setStart(start);
    ts_menuinfo.setLimit(limit);
    return this.queryList.getList("getListByTs_menuinfoPage", ts_menuinfo);
  }

  public int getCountByObj(Object obj)
  {
    Ts_menuinfo ts_menuinfo = (Ts_menuinfo)obj;
    List list = this.queryList.getList("getCountByObjTs_menuinfo", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String id) {
    return null;
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map) {
    String result_code = "0";
    try {
      Menuinfo menuinfo = new Menuinfo();
      String module_id = getFourRandnum();
      String menu_id = this.randomId.GenTradeId();
      menuinfo.setMenu_id(menu_id);
      menuinfo.setMenu_name(map.getString("menu_name"));
      menuinfo.setUp_menu_id(map.getString("up_menu_id"));
      menuinfo.setMenu_class(map.getString("menu_class"));
      menuinfo.setSubsys_code(map.getString("subsys_code"));
      menuinfo.setModule_id(module_id);
      menuinfo.setRemark(map.getString("remark"));

      String createMenu = map.getString("createMenu");

      if (createMenu.equals("1")) {
        dbmMgr.update("insertMenuinfo", menuinfo);
      }

      map.setString("result_code", result_code);
      map.setString("module_id", module_id);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    return map;
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    return null;
  }

  public String getFourRandnum() {
    String[] num = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    Random r = new Random();
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < 4; i++) {
      result.append(num[r.nextInt(10)]);
    }
    if (isExistModuleid(result.toString())) getFourRandnum();
    return result.toString();
  }

  public boolean isExistModuleid(String module_id) {
    List list = this.queryList.getList("isExistModuleid", module_id);

    return (list != null) && (list.size() > 0);
  }

  public List getListByPage(Map menuinfo)
  {
    return this.queryList.getList("getMenuinfoByObj", menuinfo);
  }

  public List getMemberListByPage(Map menuinfo) {
    return this.queryList.getList("getMemberMenuByObj", menuinfo);
  }

  public List getMenuinfoByRole(Map menuinfo) {
	  /**
	   * {menu_class=1, role_code=E66krDuS84B485i|, subsys_code=SYS, user_type=1, up_menu_id=000000000000000}
	   * 
	   * select m.menu_id,m.menu_name from ts_menuinfo m,ti_roleright r where r.menu_id=m.menu_id   

			<isNotNull prepend="" property="subsys_code">
			and m.subsys_code = #subsys_code#     
			</isNotNull>
			<isNotNull prepend="" property="up_menu_id">
			and  m.up_menu_id = #up_menu_id#     
			</isNotNull>
			<isNotNull prepend="" property="menu_class">
			and  m.menu_class = #menu_class#     
			</isNotNull>
			<isNotNull prepend="" property="role_code">
			and   INSTR(r.role_code,#role_code#)>0   
			</isNotNull>	
			
			order by m.rsrv_str0 desc
	   * */
    return this.queryList.getList("getMenuinfoByRole", menuinfo);
  }

  public List getMenuinfoByMemberCustClass(Map menuinfo) {
    return this.queryList.getList("getMenuinfoByMemberCustClass", menuinfo);
  }

  public List getMenuinfoByCustClass(Map menuinfo)
  {
    return this.queryList.getList("getMenuinfoByCustClass", menuinfo);
  }

  public List getMenuinfoByCustId(Map menuinfo) {
    return this.queryList.getList("getMenuinfoByCustId", menuinfo);
  }

  public List getMenuinfoByUpId(String Up_menu_id)
  {
    return this.queryList.getList("getMenuinfoByUpId", Up_menu_id);
  }

  public List getMenuinfoByUpIdAndRole(Map menuinfo) {
    return this.queryList.getList("getMenuinfoByUpIdAndRole", menuinfo);
  }

  public List getMenuinfoByUpIdClass(Map menuinfo) {
    return this.queryList.getList("getMenuinfoByUpIdClass", menuinfo);
  }

  public String getOneLevelFirstMenu(Map menuinfo, String user_type)
  {
    List list = null;

    if ((!user_type.equals("")) && (user_type != null)) {
      if (user_type.equals("0"))
        list = this.queryList.getList("getMenuinfoByObj", menuinfo);
      else if (user_type.equals("2"))
        list = this.queryList.getList("getMenuinfoByCustClass", menuinfo);
      else {
        list = this.queryList.getList("getMenuinfoByRole", menuinfo);
      }
    }

    String first_menu_id = "";
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = (Hashtable)list.get(0);
      if (map.get("menu_id") != null) first_menu_id = map.get("menu_id").toString();
    }

    return first_menu_id;
  }

  public String getModuleFile(String menu_id) {
    if (menu_id.equals("")) return "";
    List list = this.queryList.getList("getModuleFileByMenuId", menu_id);
    String module_path = "";
    if ((list != null) && (list.size() > 0)) {
      Hashtable mMap = (Hashtable)list.get(0);
      String module_file = ""; String module_dir = "";
      if (mMap.get("module_file") != null) module_file = mMap.get("module_file").toString();
      if (mMap.get("module_dir") != null) module_dir = mMap.get("module_dir").toString();
      module_path = module_dir + module_file;
    }
    return module_path;
  }

  public Map getDownMenu(String subsys_code, String up_menu_id, String menu_class) {
    Map menuinfo = new Hashtable();
    menuinfo.put("subsys_code", subsys_code);

    menuinfo.put("up_menu_id", up_menu_id);
    menuinfo.put("menu_class", menu_class);

    Map classMap = new Hashtable();
    String menu_id = ""; String menu_name = "";
    List list = getListByPage(menuinfo);

    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("menu_id") != null) {
          menu_id = map.get("menu_id").toString();
        }
        if (map.get("menu_name") != null) {
          menu_name = map.get("menu_name").toString();
        }

        classMap.put(menu_id, menu_name);
      }
    }

    return classMap;
  }

  public static void main(String[] args) {
    System.out.println(new MenuInfo().getFourRandnum());
  }

  public String getMenuName(String menu_id)
  {
    if (menu_id.equals("")) return "";
    List list = this.queryList.getList("getMenuName_MenuId", menu_id);
    String menu_name = "";
    if ((list != null) && (list.size() > 0)) {
      Hashtable mMap = (Hashtable)list.get(0);
      if (mMap.get("menu_name") != null) menu_name = mMap.get("menu_name").toString();
    }
    return menu_name;
  }
}